package bean;

import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import pmf.PMF;

public class ParticipateAdm {
	@SuppressWarnings("unchecked")
	public List<Participate> searchPartByArt(Articles art){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Query query = pm.newQuery(Participate.class);
		query.setFilter("article == art");
		query.declareParameters("Articles art");
		try {
			  List<Participate> results = (List<Participate>) query.execute(art);
			  List<Participate> copyResult = new LinkedList<Participate>();
			  
			  for (Participate part: results)
				  copyResult.add(new Participate(part.getKey(), null, null, part.getNeedAmount()));
			  return copyResult;
		} finally {
			  query.closeAll();
			  pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Participate> searchByAddMem(long mid){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Member mem = pm.getObjectById(Member.class, mid);
		
		Query query = pm.newQuery(Participate.class);
		query.setFilter("addMem == mem");
		query.declareParameters("Member mem");
		
		try {
			  List<Participate> results = (List<Participate>) query.execute(mem);
			  List<Participate> copyResult = new LinkedList<Participate>();
			  
			  for (Participate part: results){
				  Articles a = part.getArticle();
				  Member m = part.getAddMem();
				  copyResult.add(new Participate(part.getKey(), new Articles(a.getKey(), a.getCreateUserID(), 
						  a.getCategory(), a.getArtName(), a.getPrice(), a.getShipWay(), a.getBuyDate(), 
						  a.getBranch(), a.getTotalAmount(), a.getRemainAmount(), a.getLeftAmount(), a.getShipFee(), 
						  a.getNote()), new Member(m.getKey(), m.getUserID(), m.getName(), m.getPhone(), m.getPassword(), 
						  m.getEmail(), m.getAddress(), m.isCostcoMem(), m.isState()), part.getNeedAmount()));
			  }
			  return copyResult;
		} finally {
			  query.closeAll();
			  pm.close();
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Participate> searchOnePartByArt(Articles art){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Query query = pm.newQuery(Participate.class);
		query.setFilter("article == art");
		query.declareParameters("Articles art");
		try {
			  List<Participate> results = (List<Participate>) query.execute(art);
			  List<Participate> copyPart = new LinkedList<Participate>();
			  for(Participate p:results){
				  Member m = p.getAddMem();
			  	  Member mem = new Member(m.getKey(), m.getUserID(), m.getName(), m.getPhone(), m.getPassword(), 
			  								m.getEmail(), m.getAddress(), m.isCostcoMem(), m.isState());
			  	  copyPart.add(new Participate(p.getKey(), p.getArticle(), mem, p.getNeedAmount()));
			  }
			  return copyPart;
		} finally {
			  query.closeAll();
			  pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public Participate searchOnePartByMem(long mid, Articles art){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Member mem = pm.getObjectById(Member.class, mid);
		Query query = pm.newQuery(Participate.class);
		query.setFilter("addMem == mem");
		query.setFilter("article == art");
		query.declareParameters("Member mem, Articles art");
		try {
			  List<Participate> results = (List<Participate>) query.execute(mem, art);
			  Participate p = results.get(0);
			  //Member m = p.getAddMem();
			  //Member member = new Member(m.getKey(), m.getUserID(), m.getName(), m.getPhone(), m.getPassword(), 
			  //							m.getEmail(), m.getAddress(), m.isCostcoMem(), m.isState());
			  Participate copyPart = new Participate(p.getKey(), p.getArticle(), p.getAddMem(), p.getNeedAmount());
			  return copyPart;
		} finally {
			  query.closeAll();
			  pm.close();
		}
	}
}
